package com.crape.faq.bo.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.crape.account.bo.IAccountBO;
import com.crape.common.enums.AnswerRefType;
import com.crape.common.enums.IsMeType;
import com.crape.common.enums.QuestionStatusType;
import com.crape.common.exceptions.BOException;
import com.crape.common.model.AccountInfo;
import com.crape.common.model.Answer;
import com.crape.common.model.Question;
import com.crape.common.model.QuestionRef;
import com.crape.common.thread.ThreadObjManager;
import com.crape.faq.bo.IAnswerBO;
import com.crape.faq.bo.IQuestionBO;
import com.crape.faq.dao.IQuestionDAO;
import com.crape.faq.dao.IQuestionRefDAO;

@Service
public class QuestionBOImpl implements IQuestionBO {

	private IQuestionDAO questionDAOImpl;
	private IAnswerBO answerBOImpl;
	private IAccountBO accountBOImpl;
	private IQuestionRefDAO questionRefDAOImpl;

	@Autowired
	public void setquestionDAOImpl(IQuestionDAO questionDAOImpl) {
		this.questionDAOImpl = questionDAOImpl;
	}

	@Autowired
	public void setAnswerBOImpl(IAnswerBO answerBOImpl) {
		this.answerBOImpl = answerBOImpl;
	}

	@Autowired
	public void setAccountBOImpl(IAccountBO accountBOImpl) {
		this.accountBOImpl = accountBOImpl;
	}

	@Autowired
	public void setQuestionRefDAOImpl(IQuestionRefDAO questionRefDAOImpl) {
		this.questionRefDAOImpl = questionRefDAOImpl;
	}
	
	@Override
	public List<Question> searchQuestions(String searchValue, Integer startIndex, Integer pageSize) throws BOException {
		if (startIndex == null || pageSize == null) {
			return null;
		}
		if (searchValue == null || searchValue.isEmpty()) {
			searchValue = null;
		}
		List<Question> questionList = questionDAOImpl.searchQuestions(searchValue, startIndex, pageSize);
		if (questionList == null || questionList.isEmpty()) {
			return null;
		}
		for (Question question : questionList) {
			AccountInfo accountInfo = accountBOImpl.getAccountInfoById(question.getAccountid());
			question.setAccountInfo(accountInfo);
		}
		return questionList;
	}

	@Override
	public Long countSearchQuestions(String searchValue) throws BOException {
		if (searchValue == null || searchValue.isEmpty()) {
			searchValue = null;
		}
		return questionDAOImpl.countSearchQuestions(searchValue);
	}

	@Override
	public List<Question> getQuestions(Question question) throws BOException {
		if (question == null) {
			return null;
		}
		return questionDAOImpl.listQuestions(question);
	}

	@Override
	public Question getQuestionById(Long id) throws BOException {
		if (id == null) {
			return null;
		}
		Question param = new Question();
		param.setId(id);
		List<Question> list = questionDAOImpl.listQuestions(param);
		if (list == null || list.isEmpty()) {
			return null;
		}
		Question question = list.get(0);
		Answer answer = answerBOImpl.getTheAnswer(id);
		question.setAnswer(answer);
		Long meid = ThreadObjManager.getLoginUser().getAccountid();
		if(meid!=null&&meid.compareTo(question.getAccountid())==0){
			question.setIsMe(IsMeType.YES.getCode());
		}else{
			question.setIsMe(IsMeType.NO.getCode());
		}
		if(answer!=null){
			List<AccountInfo> refAccounts = accountBOImpl.getPagedAccountByAnswerRef(answer.getId(), AnswerRefType.zan.getCode(), 0, 6);
			answer.setRefAccounts(refAccounts);
		}
		return question;
	}

	@Override
	public List<Question> getPagedQuestions(Integer startIndex, Integer pageSize) throws BOException {
		if (startIndex == null || pageSize == null || pageSize == 0) {
			return null;
		}
		List<Question> questionList = questionDAOImpl.getPagedQuestions(startIndex, pageSize);
		if (questionList == null || questionList.isEmpty()) {
			return null;
		}
		for (Question question : questionList) {
			AccountInfo accountInfo = accountBOImpl.getAccountInfoById(question.getAccountid());
			question.setAccountInfo(accountInfo);
			Answer answer = answerBOImpl.getTheAnswer(question.getId());
			question.setAnswer(answer);
			Long meid = ThreadObjManager.getLoginUser().getAccountid();
			if(meid!=null&&meid.compareTo(question.getAccountid())==0){
				question.setIsMe(IsMeType.YES.getCode());
			}else{
				question.setIsMe(IsMeType.NO.getCode());
			}
		}
		return questionList;
	}

	@Override
	public Long countPageQuestions() throws BOException {
		Question question = new Question();
		question.setStatus(QuestionStatusType.published.getCode());
		return questionDAOImpl.countQuestions(question);
	}

	@Override
	public List<Question> getPagedQuestionsByAccountid(Long accountid, Integer startIndex, Integer pageSize) throws BOException {
		if (accountid == null || startIndex == null || pageSize == null || pageSize == 0) {
			return null;
		}
		List<Question> questionList = questionDAOImpl.getPagedQuestionsByAccountid(accountid, startIndex, pageSize);
		if (questionList == null || questionList.isEmpty()) {
			return null;
		}
		AccountInfo accountInfo = accountBOImpl.getAccountInfoById(accountid);
		for (Question question : questionList) {
			question.setAccountInfo(accountInfo);
			Answer answer = answerBOImpl.getTheAnswer(question.getId());
			question.setAnswer(answer);
			Long meid = ThreadObjManager.getLoginUser().getAccountid();
			if(meid!=null&&meid.compareTo(question.getAccountid())==0){
				question.setIsMe(IsMeType.YES.getCode());
			}else{
				question.setIsMe(IsMeType.NO.getCode());
			}
		}
		return questionList;
	}

	@Override
	public Long countPageQuestionsByAccountid(Long accountid) throws BOException {
		if (accountid == null) {
			return 0L;
		}
		Question question = new Question();
		question.setAccountid(accountid);
		question.setStatus(QuestionStatusType.published.getCode());
		return questionDAOImpl.countQuestions(question);
	}

	@Override
	public List<Question> getOther10Questions(Long questionid) throws BOException {
		if (questionid == null) {
			return null;
		}
		return questionDAOImpl.getOther10Questions(questionid);
	}

	@Override
	public List<Question> getHotQuestions() throws BOException {
		List<Question> questionList = questionDAOImpl.getHotQuestions();
		for (Question question : questionList) {
			AccountInfo accountInfo = accountBOImpl.getAccountInfoById(question.getAccountid());
			question.setAccountInfo(accountInfo);
		}
		return questionList;
	}

	@Override
	public Long countAnswerQuestionsByAccountid(Long accountid) throws BOException {
		if (accountid == null) {
			return 0L;
		}
		return questionDAOImpl.countAnswerQuestionsByAccountid(accountid);
	}

	@Override
	public List<Question> getPagedAnswerQuestionsByAccountid(Long accountid, Integer startIndex, Integer pageSize) throws BOException {
		if (accountid == null || startIndex == null || pageSize == null || pageSize == 0) {
			return null;
		}
		List<Question> questionList = questionDAOImpl.getPagedAnswerQuestionsByAccountid(accountid, startIndex, pageSize);
		if (questionList == null || questionList.isEmpty()) {
			return null;
		}
		AccountInfo accountInfo = accountBOImpl.getAccountInfoById(accountid);
		for (Question question : questionList) {
			question.setAccountInfo(accountInfo);
			Answer answer = answerBOImpl.getTheAnswer(question.getId());
			question.setAnswer(answer);
			Long meid = ThreadObjManager.getLoginUser().getAccountid();
			if(meid!=null&&meid.compareTo(question.getAccountid())==0){
				question.setIsMe(IsMeType.YES.getCode());
			}else{
				question.setIsMe(IsMeType.NO.getCode());
			}
		}
		return questionList;
	}
	
	@Override
	public Long countFollowedQuestionsByAccountid(Long accountid) throws BOException {
		if (accountid == null) {
			return 0L;
		}
		return questionDAOImpl.countFollowedQuestionsByAccountid(accountid);
	}

	@Override
	public List<Question> getPagedFollowedQuestionsByAccountid(Long accountid, Integer startIndex, Integer pageSize) throws BOException {
		if (accountid == null || startIndex == null || pageSize == null || pageSize == 0) {
			return null;
		}
		List<Question> questionList = questionDAOImpl.getPagedFollowedQuestionsByAccountid(accountid, startIndex, pageSize);
		if (questionList == null || questionList.isEmpty()) {
			return null;
		}
		AccountInfo accountInfo = accountBOImpl.getAccountInfoById(accountid);
		for (Question question : questionList) {
			question.setAccountInfo(accountInfo);
			Answer answer = answerBOImpl.getTheAnswer(question.getId());
			question.setAnswer(answer);
			Long meid = ThreadObjManager.getLoginUser().getAccountid();
			if(meid!=null&&meid.compareTo(question.getAccountid())==0){
				question.setIsMe(IsMeType.YES.getCode());
			}else{
				question.setIsMe(IsMeType.NO.getCode());
			}
		}
		return questionList;
	}

	@Override
	public void updateQuestion(Question question) throws BOException {
		if (question == null) {
			return;
		}
		questionDAOImpl.updateQuestion(question);
		if (question.getStatus() == QuestionStatusType.published.getCode()) {
			accountBOImpl.statAccountQuestion(question.getAccountid());
		}
	}

	@Override
	public void addQuestion(Question question) throws BOException {
		if (question == null) {
			return;
		}
		questionDAOImpl.insertQuestion(question);
		if (question.getStatus() == QuestionStatusType.published.getCode()) {
			accountBOImpl.statAccountQuestion(question.getAccountid());
		}
	}

	@Override
	public void addQuestionAttention(Long accountid, Long questionid) {
		QuestionRef questionRef = questionRefDAOImpl.queryQuestionRef(accountid, questionid);
		if (questionRef == null) {
			questionRefDAOImpl.insertQuestionRef(accountid, questionid);
		}
		this.recountQuestionAttention(accountid, questionid);
	}

	@Override
	public void cancelQuestionAttention(Long accountid, Long questionid) {
		QuestionRef questionRef = questionRefDAOImpl.queryQuestionRef(accountid, questionid);
		if (questionRef != null) {
			questionRefDAOImpl.backupQuestionRef(accountid, questionid);
			questionRefDAOImpl.deleteQuestionRef(accountid, questionid);
		}
		this.recountQuestionAttention(accountid, questionid);
	}
	
	private void recountQuestionAttention(Long accountid, Long questionid){
		Long count = questionRefDAOImpl.countQuestionRefByQuestionid(questionid);
		questionDAOImpl.updateQuestionAttentions(questionid, count);
	}

	@Override
	public Long countQuestionRefByQuestionid(Long questionid) throws BOException {
		return questionRefDAOImpl.countQuestionRefByQuestionid(questionid);
	}
	
}
